|
1
|
|
|
import { AngularFireAuth } from "@angular/fire/auth"; |
|
2
|
|
|
import { Platform } from "@ionic/angular"; |
|
3
|
|
|
import { auth } from "firebase"; |
|
4
|
|
|
import { UniFirebaseLoginConfig } from "../config/uni-firebase-login-config"; |
|
5
|
|
|
import { UniFirebaseLoginConfigProvider } from "../config/uni-firebase-login-config-provider"; |
|
6
|
|
|
import { IAuthProvider } from "./i-auth-provider"; |
|
7
|
|
|
|
|
8
|
|
|
export abstract class AbstractAuth implements IAuthProvider { |
|
9
|
|
|
public abstract readonly providerKey: |
|
10
|
|
|
| "anonymous" |
|
11
|
|
|
| "email" |
|
12
|
|
|
| "facebook" |
|
13
|
|
|
| "github" |
|
14
|
|
|
| "google" |
|
15
|
|
|
| "phone" |
|
16
|
|
|
| "twitter"; |
|
17
|
|
|
public readonly defaultOptions: any = {}; |
|
18
|
|
|
protected config: UniFirebaseLoginConfig; |
|
19
|
|
|
|
|
20
|
|
|
protected constructor( |
|
21
|
|
|
protected angularFireAuth: AngularFireAuth, |
|
22
|
|
|
protected platform: Platform, |
|
23
|
|
|
configProvider: UniFirebaseLoginConfigProvider, |
|
24
|
|
|
) { |
|
25
|
|
|
this.config = configProvider.config; |
|
26
|
|
|
this.applyDefaultOptions(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public abstract signInNative( |
|
30
|
|
|
options: any, |
|
31
|
|
|
): Promise<auth.UserCredential | null>; |
|
32
|
|
|
|
|
33
|
|
|
public async signIn( |
|
34
|
|
|
options: any = {}, |
|
35
|
|
|
): Promise<auth.UserCredential | null> { |
|
36
|
|
|
if (this.platform.is("cordova")) { |
|
37
|
|
|
return this.signInNative(options); |
|
38
|
|
|
} else { |
|
39
|
|
|
return this.signInBrowser(); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public async signOutNative(): Promise<void> { |
|
44
|
|
|
if (this.angularFireAuth.auth.currentUser === null) { |
|
45
|
|
|
console.warn("Unknown currentUser!"); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public async signOutBrowser(): Promise<void> { |
|
50
|
|
|
return this.angularFireAuth.auth.signOut(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public async signOut(): Promise<void> { |
|
54
|
|
|
if (this.platform.is("cordova")) { |
|
55
|
|
|
return this.signOutNative(); |
|
56
|
|
|
} else { |
|
57
|
|
|
return this.signOutBrowser(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public async signInBrowser(): Promise<auth.UserCredential | null> { |
|
62
|
|
|
const provider = this.getBrowserSignInProvider(); |
|
63
|
|
|
const authX = this.angularFireAuth.auth; |
|
64
|
|
|
|
|
65
|
|
|
switch (this.defaultOptions.signInType) { |
|
66
|
|
|
case "popup": |
|
67
|
|
|
return await authX.signInWithPopup(provider); |
|
68
|
|
|
case "redirect": |
|
69
|
|
|
await authX.signInWithRedirect(provider); |
|
70
|
|
|
// TODO: implement redirect resolution: https://stackoverflow.com/questions/40219478/firebaseauth-googleauthprovider-signinwithredirect |
|
71
|
|
|
throw new Error("Not implemented!"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
throw new Error("Invalid signInType!"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Apply default options to configuration for the provider |
|
79
|
|
|
*/ |
|
80
|
|
|
protected applyDefaultOptions(): any { |
|
81
|
|
|
if (this.config.providers[this.providerKey] === undefined) { |
|
82
|
|
|
this.config.providers[this.providerKey] = {}; |
|
83
|
|
|
} |
|
84
|
|
|
return Object.assign( |
|
85
|
|
|
this.config.providers[this.providerKey], |
|
86
|
|
|
this.defaultOptions, |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected abstract getBrowserSignInProvider(): any | null; |
|
91
|
|
|
} |
|
92
|
|
|
|